package com.example.qincetest

import android.graphics.drawable.Drawable
import android.graphics.drawable.PictureDrawable
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.Toast
import coil3.ImageLoader
import coil3.load
import coil3.request.ImageRequest
import coil3.request.crossfade
import coil3.request.transformations
import coil3.transform.CircleCropTransformation
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition
import com.caverock.androidsvg.SVG
import com.example.qincetest.widgets.svg.SvgDecoder
import com.example.qincetest.widgets.svg.SvgDrawableTranscoder
import jp.wasabeef.glide.transformations.BlurTransformation
import timber.log.Timber
import java.io.InputStream

class GlideTestActivity : BaseActivity() {

    private lateinit var imageView: ImageView
    private lateinit var progressBar: ProgressBar
    private var retryCount = 0
    private val maxRetries = 3
    private val imageUrl = "https://universal-test-1301203089.file.myqcloud.com/content/fd1757018d6c1666abfe839e834305a5/e20527ac6a3557bf31657218d84edac3.gif"
    private val svgPath = "https://www.clker.com/cliparts/u/Z/2/b/a/6/android-toy-h.svg"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

//        Glide.get(this).registry
//            .register(SVG::class.java, PictureDrawable::class.java, SvgDrawableTranscoder())
//            .append(InputStream::class.java, SVG::class.java, SvgDecoder())

        setContentView(R.layout.activity_glide_test)

        imageView = findViewById(R.id.imageView)
        progressBar = findViewById(R.id.progressBar)

        loadImage()
        loadImage2()

        Glide.with(baseContext).load(svgPath).into(findViewById(R.id.imageViewSVG))

        val target = object : CustomTarget<Drawable>(){
            override fun onResourceReady(p0: Drawable, p1: Transition<in Drawable>?) {
                Log.e("zfl","onResourceReady")
                imageView.setImageDrawable(p0)
            }

            override fun onLoadCleared(p0: Drawable?) {
                Log.e("zfl","onLoadCleared")
            }

            override fun onLoadFailed(errorDrawable: Drawable?) {
                Log.e("zfl","onLoadFailed")
            }

        }
        Glide.with(baseContext).load(svgPath).into(target)
    }

    private fun loadImage2() {
        val request = ImageRequest.Builder(this)
            .data(imageUrl)
            .size(400, 300) // 设置图像的目标尺寸
            //.placeholder(getR.drawable) // 设置占位图
            //.error(R.mipmap.ic_avatar) // 设置加载错误时显示的图像
            .build()

        findViewById<ImageView>(R.id.imageView2).load(imageUrl){
            crossfade(true)
//            placeholder(R.drawable.ic_big_pic)
            transformations(CircleCropTransformation())
        }
    }

    private fun loadImage() {
        //val imageUrl = "https://up.zhuoku.org/22/a4/60/50/fc3bd0b4e656911fccdde4383637c1cd.jpg"
        progressBar.visibility = View.VISIBLE

        /* 打开代理后可能加载失败，配置networkSecurityConfig即可
        * */
        Glide.with(this)
            .load(imageUrl)
            .transition(DrawableTransitionOptions.withCrossFade())
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .error(R.mipmap.ic_launcher)
            .listener(object : RequestListener<Drawable> {
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>,
                    isFirstResource: Boolean
                ): Boolean {
                    Timber.e(e?.message)
                    if (retryCount < maxRetries) {
                        retryCount++
                        Timber.d("Retrying image load attempt $retryCount")
                        loadImage()
                        return true
                    }
                    progressBar.visibility = View.GONE
                    Toast.makeText(this@GlideTestActivity, "图片加载失败", Toast.LENGTH_SHORT).show()
                    return false
                }

                override fun onResourceReady(
                    resource: Drawable,
                    model: Any,
                    target: Target<Drawable>?,
                    dataSource: DataSource,
                    isFirstResource: Boolean
                ): Boolean {
                    progressBar.visibility = View.GONE
                    retryCount = 0
                    return false
                }
            })//.apply(RequestOptions.bitmapTransform(BlurTransformation(5, 3)))
            .into(imageView)
    }
}